//
//  CZQMainController.m
//  weibo
//
//  Created by Apple on 15-1-29.
//  Copyright (c) 2015年 itcast. All rights reserved.
//

#import "CZQMainController.h"
#import "CZQHomeController.h"
#import "CZQMessageController.h"
#import "CZQDiscoverController.h"
#import "CZQProfileController.h"
#import "CZQTabBar.h"
@interface CZQMainController ()<TabBarDelegate>
/**
 自定义tabBar
 */
@property(nonatomic,weak)CZQTabBar *custonTabBar;
@end

@implementation CZQMainController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{

    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        //创建首页子控制器
        UIViewController *mainCtr = [self addViewControllerClass:[CZQHomeController class] title:@"首页" norImage:@"tabbar_home" selImage:@"tabbar_home_selected"];
        
        //创建发现子控制器
        UIViewController *messageCtr = [self addViewControllerClass:[CZQMessageController class] title:@"消息" norImage:@"tabbar_message_center" selImage:@"tabbar_message_center_selected"];
        
        //创建消息子控制器
        UIViewController *disCtr = [self addViewControllerClass:[CZQDiscoverController class] title:@"发现" norImage:@"tabbar_discover" selImage:@"tabbar_discover_selected"];
        
        //创建我的子控制器
        UIViewController *meCtr = [self addViewControllerClass:[CZQProfileController class] title:@"我" norImage:@"tabbar_profile" selImage:@"tabbar_profile_selected"];
    }
    return self ;

}
//自定义tabBar
- (void)viewDidLoad{
    
    [super viewDidLoad];
    
    CZQTabBar *tabBar = [[CZQTabBar alloc] init];
    //设置tabBar的frame
    tabBar.frame = self.tabBar.frame;
    //添加到当前控制器中
    [self.view  addSubview:tabBar];
    
    tabBar.delegate = self;
     // 移除系统的tabBar
    [self.tabBar removeFromSuperview];
    
    self.custonTabBar = tabBar;
    



}
- (UIViewController *)addViewControllerClass:(Class)Class title:(NSString *)title norImage:(NSString *)norImage selImage:(NSString *)selImage{
    //并不是所有的控制器都通过init的方式创建的
    //所以可以通过传递一个控制器  来进行相应的属性进行初始化
    UIViewController *vc = [[Class alloc] init];
    vc.view.backgroundColor = randomColor;
    return [self addViewController:vc title:title norImage:norImage selImage:selImage];
}


- (UIViewController *)addViewController:(UIViewController *)Controller title:(NSString *)title norImage:(NSString *)norImage selImage:(NSString *)selImage{
    
    Controller.tabBarItem.title = title;
    Controller.navigationItem.title = title;
    Controller.tabBarItem.image = [UIImage imageNamed:norImage];
    //选中图片的时候  不对图片进行渲染
    UIImage *selRenderImg =[UIImage imageNamed:selImage];
    selRenderImg = [selRenderImg imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    
    
    Controller.tabBarItem.selectedImage = selRenderImg;
    
    UINavigationController *Nav = [[UINavigationController alloc] initWithRootViewController:Controller];
    
    
    [self  addChildViewController:Nav];
    
    // 每添加一个子控制器就要创建一个对应的按钮
    [self.custonTabBar addItem:Controller.tabBarItem];
    
    return Controller;
    
}
- (void)tabBar:(CZQTabBar *)tabBar from:(NSInteger)form to:(NSInteger)to{

    NSLog(@"from = %tu  to = %tu", form, to);
    self.selectedIndex = to;

}

@end
